<?php
/**
 * wap 2.0 用户中心 －订单列表
 */

$now = local_time();

$filter = empty($_REQUEST['filter']) ? 'uncompleted' : trim($_REQUEST['filter']);

$orders = array();


if (!empty($orders) && is_array($orders))
{
	// 修正订单状态显示
	foreach ($orders AS $key => $val)
	{
		if (!is_array($val)){
			unset($orders[$key]);
			continue;
		}

		// 下单1小时内订单显示为 “待审核订单”
		if ($now - $val['add_time'] < 3600 && $orders[$key]['order_status'] == 10 && $orders[$key]['pay_type'] != 8){
			$orders[$key]['order_status'] = 1;
			$orders[$key]['order_status_name'] = '已支付，待审核订单';
		}
		else if ($now - $val['add_time'] < 3600  && $orders[$key]['order_status'] == 10)
		{
			$orders[$key]['order_status'] = 1;
			$orders[$key]['order_status_name'] = '待审核订单';
		}

		// 不显示 用户已删除订单
		if (in_array($val['order_status'], array(9, 99) ))
		{
			unset($orders[$key]);
		}
		else
		{
			$orders[$key]['add_time'] = date('Y-m-d H:i:s', $val['add_time']);
		}
		$orders[$key]['money'] = intval($orders[$key]['money']);
	}
}
else
{
	$orders = array();
}


/* 未完成订单 */
if ($filter == 'uncompleted')
{
	$completed = array(25,60,97,70,49,59);
	foreach ($orders AS $key => $val)
	{
		if (in_array($val['order_status'], $completed)){
			unset($orders[$key]);
		}
	}
}

// 待付款订单
else if ($filter == 'unpay')
{
	foreach ($orders AS $key => $val)
	{
		if ($val['order_status'] != 0)
		{
			unset($orders[$key]);
		}
	}

	$smarty->assign('unshow', 	1);
}

// 待审核订单
else if ($filter == 'unverify')
{
	$merge_count = 0;
	foreach ($orders AS $key => $val)
	{
		if ($val['order_status'] == 1
		|| ($now - strtotime($val['add_time']) < 3600  && $val['order_status'] == 10))
		{
			//
		}else{
			unset($orders[$key]);
		}

		if($val['can_merge']=='1'){
			$merge_count++;
		}
	}

	$smarty->assign('unshow', 	1);
	$smarty->assign('merge_count', 	$merge_count);
}

// 待发货订单
else if ($filter == 'unsend')
{
	foreach ($orders AS $key => $val)
	{
		if ($val['order_status'] == 20	|| $val['order_status'] == 10
		|| $val['order_status'] == 21)
		{
		}
		else{
			unset($orders[$key]);
		}
	}

	$smarty->assign('unshow', 	1);
}

// 已发货订单
else if ($filter == 'sended')
{
	foreach ($orders AS $key => $val)
	{
		if ($val['order_status'] == 22 || $val['order_status'] == 44)
		{
		}else{
			unset($orders[$key]);
		}
	}

	$smarty->assign('unshow', 	1);
}

// 退货订单
else if ($filter == 'return')
{
	//22 - 已发货 25 - 已签收 49 - 已退款 54 - 退货已审核 53 - 退货未审核
	$return_array = array(22,25,49,53,54);
	foreach ($orders AS $key => $val)
	{
		if (!in_array($val['order_status'],$return_array)){
			unset($orders[$key]);
		}
	}

	$smarty->assign('unshow', 	1);
}

$count = count($orders);
$page > ceil($count/$perpage) && $page = ceil($count/$perpage);

$orders = array_slice($orders, ($page-1) * $perpage, $perpage);

// 定义不显示物流查看状态（已取消、已退款、已完成、已拒收）
$disable_logistics = array(97, 49,59, 60, 70);

/* 再次支付 */
foreach ((array)$orders AS $key => $val)
{
	if ($val['order_status'] == 0 && ($val['pay_type'] == 49 || $val['pay_type'] == 50))
	{
		if ( (defined('OPEN_MOBILE_PAY') && OPEN_MOBILE_PAY != 'off')
		|| (defined('OPEN_ALIPAY_PAY') && OPEN_ALIPAY_PAY != 'off') )
		{
			$isRepay = $mem->getCache('sexmall_order_unpay_'.$val['order_sn'], true);
			if (empty($isRepay))
			{
				$isRepay = $mInterface->userOrderRepay($user_token, $val['order_sn']);
				$mem->addCache('sexmall_order_unpay_'.$val['order_sn'], $isRepay, 10, true);
			}
			if (!empty($isRepay) && is_array($isRepay) && $isRepay['code'] == 1)
			{
				if ($val['pay_type'] == 49)
				{
					$url = MOBILE_PAY_URL;
					$verify_key = MOBILE_PAY_VERIFY_KEY;
				}
				else if ($val['pay_type'] == 50)
				{
					//$url = ALIPAY_PAY_URL;
					$url = 'http://pay.sexmall.com/public/operate.php';
					$verify_key = ALIPAY_PAY_VERIFY_KEY;
				}

				/*$data = array(
					'order_sn'		=> $val['order_sn'],
					'order_money'	=> $val['money'],
					'verify'		=> substr(md5($val['order_sn'].$val['money'].$verify_key),1, 10),
					'bank_id'		=> ''
					);*/

				$data = array(
					'orders'	=> $val['order_sn'],
					'pay_type'	=> $val['pay_type'],
					'pay_id'	=> 1,
					'bank_id'	=> ''
					);

					$orders[$key]['repay'] = $url . '?' . http_build_query($data);
			}
		}
	}

	//7天内退货标识
	if(($now - strtotime($val['add_time']))<=( 7 * 86400 )){
		$orders[$key]['dis'] = 1;
	}

	if ( $val['order_status'] > 10 && !in_array($val['order_status'], $disable_logistics) )
	{
		$orders[$key]['show_logistics'] = 1;
	}

	//mark可评价订单
	$order_status = get_order_feedback_status();
	if(in_array($val['order_status'],$order_status)){
		//查询订单是否可评价
		if(($now - strtotime($val['add_time']))<=( 25 * 86400 )){
			$get_order_feedback_status = $mInterface->checkFeedbackStatus($user_token, $val['order_sn'] , $val['order_status']);
			if($get_order_feedback_status['code'] == '0'){
				$orders[$key]['is_feedback'] = 1;
			}
		}
	}
}


/* 分页 */
$params = array('m'=>'user','act'=>$action, 'step'=>$step, 'filter'=>$filter);
$pager = get_pager('index.php', $params, $count, $page, $perpage);

/* OMNITURE 统计  */
$pageName = '用户中心-订单列表';
include 'omniture/other.php';

wap_setcookie("WAP[back_act]", 'index.php?m=user&act=order&step=list&filter='.$filter);

$smarty->assign('pager', $pager);

$smarty->assign('orders', $orders);
$smarty->assign('count', count($orders));
$smarty->assign('step', $step);
$smarty->assign('filter', $filter);

$smarty->display('user_order_list.dwt');
